[typing] Add missing _from_iterable classmethod to Set ABCs#15331
[typing] Add missing _from_iterable classmethod to Set ABCs#15331srittau merged 4 commits intopython:mainfrom
_from_iterable classmethod to Set ABCs#15331Conversation
Signed-off-by: Emmanuel Ferdman <emmanuelferdman@gmail.com>
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
stdlib/typing.pyi
Outdated
| class ItemsView(MappingView, AbstractSet[tuple[_KT_co, _VT_co]], Generic[_KT_co, _VT_co]): | ||
| def __init__(self, mapping: SupportsGetItemViewable[_KT_co, _VT_co]) -> None: ... # undocumented | ||
| @classmethod | ||
| def _from_iterable(cls, it: Iterable[Any]) -> set[Any]: ... |
There was a problem hiding this comment.
Why not def [S](cls, it: Iterable[S]) -> set[S] ?
There was a problem hiding this comment.
Good call, updated to use Iterable[_S] -> set[_S]. Used the existing _S TypeVar since PEP 695 syntax isn't used elsewhere in typeshed.
stdlib/typing.pyi
Outdated
| def _hash(self) -> int: ... | ||
| # Mixin methods | ||
| @classmethod | ||
| def _from_iterable(cls, it: Iterable[Any]) -> AbstractSet[Any]: ... |
There was a problem hiding this comment.
This should return Self, not any AbstractSet, since the implementation is return cls(it) and other methods on MutableSet that rely on this are annotated to return Self.
There was a problem hiding this comment.
Updated to AbstractSet[_S] for type preservation, but can't use Self here - KeysView and ItemsView override this to return set, not Self. Using Self on the base would make those overrides invalid. Also MutableSet.__ior__/__iand__ etc. don't actually call _from_iterable, they mutate in-place directly.
There was a problem hiding this comment.
Actually, the type we want here is more like def [S](cls, Iterable[S]) -> Self & AbstractSet[S], which is not expressible at the moment.
There was a problem hiding this comment.
So -> AbstractSet[S] seems like the best approximation atm.
Signed-off-by: Emmanuel Ferdman <emmanuelferdman@gmail.com>
This comment has been minimized.
This comment has been minimized.
|
Btw. in the original issue I posted a comment that this is probably a bad idea at the moment -- sorry I didn't mention it here earlier, I didn't realize this was a different account than the OP of #15298 |
|
So, cpython will not support Therefore, the best we can do is put that definition into a |
| @classmethod | ||
| def _from_iterable(cls, it: Iterable[_S]) -> AbstractSet[_S]: ... |
There was a problem hiding this comment.
| @classmethod | |
| def _from_iterable(cls, it: Iterable[_S]) -> AbstractSet[_S]: ... | |
| if not TYPE_CHECKING: | |
| # this method exists on collections.abc.Set, but since it is not | |
| # supported by builtins.set, we exclude it from static typing | |
| # see: cpython/issues/144557 and typeshed/issues/15298 | |
| @classmethod | |
| def _from_iterable(cls, it: Iterable[_S]) -> AbstractSet[_S]: ... |
| @classmethod | ||
| def _from_iterable(cls, it: Iterable[_S]) -> set[_S]: ... |
There was a problem hiding this comment.
| @classmethod | |
| def _from_iterable(cls, it: Iterable[_S]) -> set[_S]: ... | |
| if not TYPE_CHECKING: | |
| # this method exists on collections.abc.ItemsView, but since it is not | |
| # supported by dict_items, we exclude it from static typing | |
| # see: cpython/issues/144557 and typeshed/issues/15298 | |
| @classmethod | |
| def _from_iterable(cls, it: Iterable[_S]) -> set[_S]: ... |
| @classmethod | ||
| def _from_iterable(cls, it: Iterable[_S]) -> set[_S]: ... |
There was a problem hiding this comment.
| @classmethod | |
| def _from_iterable(cls, it: Iterable[_S]) -> set[_S]: ... | |
| if not TYPE_CHECKING: | |
| # this method exists on collections.abc.KeysView, but since it is not | |
| # supported by dict_keys, we exclude it from static typing | |
| # see: cpython/issues/144557 and typeshed/issues/15298 | |
| @classmethod | |
| def _from_iterable(cls, it: Iterable[_S]) -> set[_S]: ... |
I don't think we should start putting |
|
Diff from mypy_primer, showing the effect of this PR on open source code: discord.py (https://github.com/Rapptz/discord.py)
- ...typeshed_to_test/stdlib/typing.pyi:1049: note: "update" of "TypedDict" defined here
+ ...typeshed_to_test/stdlib/typing.pyi:1055: note: "update" of "TypedDict" defined here
|
PR Summary
This PR adds
_from_iterabletoAbstractSet(returningAbstractSet[_S]to preserve element types while allowing subclasses that return set), and toKeysView/ItemsViewwhich override it to returnset[_S].MutableSetinherits fromAbstractSetso no separate definition is needed.Fixes #15298.